package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class addressBookController {
  @Autowired
  private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result saveAddress(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressBookService.saveAddress(addressBook);
        return Result.success();

    }

    /**
     * 查询用户地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddress(){
        log.info("查询地址");
        List<AddressBook> list =addressBookService.getAddress();
        return Result.success(list);
    }

    /**
     * 根据id查询地址-查询回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址:{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址修改地址
     */
    @PutMapping
    public Result updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     */
    @DeleteMapping
    public Result deleteAddress(Long id){
        log.info("删除地址:{}",id);
        addressBookService.deleteAddress(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public Result setDefault( @RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        log.info("根据id查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }
}

